// pages/sports_item/index.js
const util=require('../../static/util')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    item:'',
    timerStr:'05:00',
    isflag: true,
    Time: '5',
    timem:null,
    mTime: 300000,
    height: 0,
    time: '25',
    rate: '',
    pauseshow:true,
    startshow:true,
    continuecancleshow:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
   console.log(JSON.parse(decodeURIComponent(options.item)))
   this.setData({
       item:JSON.parse(decodeURIComponent(options.item))
   })
   var res = wx.getSystemInfoSync();
   var rate = 750 / res.windowWidth;
   this.setData({
     rate: rate,
     height: rate * res.windowHeight
   });
   console.log(util.formatTime(new Date()))
  },
  start: function () {
    this.setData({
      isflag: false,
      startshow:false,
      mTime: this.data.time * 60 * 1000,
      timerStr:parseInt(this.data.time)>=10?this.data.time+':00':'0'+this.data.time+':00'
    });
    this.drag();
    this.dragac();
  },
  drag: function () {
    var linewidth = 6 / this.data.rate;
    var ctx = wx.createCanvasContext('timer_bg');
    ctx.setLineWidth(linewidth);
    ctx.setStrokeStyle('#000000');
    ctx.setLineCap('round');
    ctx.beginPath();
    ctx.arc(400 / this.data.rate / 2, 400 / this.data.rate / 2, 400 / this.data.rate / 2 - 2 * linewidth, 0, 2 * Math.PI, false);
    ctx.stroke();
    ctx.draw();
  },
  dragac: function () {
    var that = this; // 保存正确的 this 上下文
    var timem = setInterval(function () {
     var currenttime=that.data.mTime-100;
      var angle =1.5 + 2*(that.data.time * 60 * 1000 - that.data.mTime) / (that.data.time * 60 * 1000);
      that.setData({
          mTime:currenttime
      })
      if(angle<3.5){
          if(currenttime%1000===0){
              var timeStr1=currenttime/1000;
              var timeStr2=parseInt(timeStr1/60);
              var timeStr3=(timeStr1-timeStr2*60)>=10?(timeStr1-timeStr2*60):'0'+(timeStr1-timeStr2*60);
              var timeStr2=timeStr2>=10?timeStr2:'0'+timeStr2
              that.setData({
                  timerStr:timeStr2+':'+timeStr3
              })
          }
        var linewidth = 6 / that.data.rate;
        var ctx = wx.createCanvasContext('timer_ac');
        ctx.setLineWidth(linewidth);
        ctx.setStrokeStyle('#ffffff');
        ctx.setLineCap('round');
        ctx.beginPath();
        ctx.arc(400 / that.data.rate / 2, 400 / that.data.rate / 2, 400 / that.data.rate / 2 - 2 * linewidth, 1.5* Math.PI, angle * Math.PI, false);
        ctx.stroke(); 
        ctx.draw();
      }else{

      var logs=wx.getStorageSync('logs')||[]
      logs.unshift({
          date:util.formatTime(new Date()),
          cate:that.data.cate,
          time:that.data.time
      })
      wx.setStorageSync('logs', logs)

      that.setData({
          timerStr:'00:00',
          okshow:true,
          pauseshow:false,
          continuecancleshow:false
      })
      clearInterval(timem)
      }
     
    }, 100); 
    that.setData({
        timem:timem
    })
  },
//   ok:function(){
//     clearInterval(this.data.timem)
//     this.setData({
//         pauseshow:true,
//         continuecancleshow:false,
//         okshow:false,
//         isflag:true
//     })
//   },
  pause:function(){
   clearInterval(this.data.timem)
   this.setData({
   pauseshow:false,
   continuecancleshow:true,
   okshow:false
   })
  
  },
  continue:function(){
    this.dragac()
    this.setData({
        pauseshow:true,
        continuecancleshow:false,
        okshow:false
    })

  },
  cancle:function(){
   clearInterval(this.data.timem)
   this.setData({
       pauseshow:true,
       continuecancleshow:false,
       okshow:false,
       isflag:true
   })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})